package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.AttendanceMonthlyAddDTO;
import org.jsola.hr.dto.EmpSecretKeysUpdateDTO;
import org.jsola.hr.dto.EmpSocChangesAddDTO;
import org.jsola.hr.dto.EmpSocChangesUpdateDTO;
import org.jsola.hr.query.EmpSocChangesQuery;
import org.jsola.hr.service.IEmpSecretKeysService;
import org.jsola.hr.service.IEmpSocChangesService;
import org.jsola.hr.vo.EmpSocChangesListVO;
import org.jsola.hr.vo.EmpSocChangesVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrEmpSocChangesController")
@RequestMapping(HrConstants.API_PATH + "/v1/empSocChanges")
public class EmpSocChangesController implements IEmpSocChangesControllerAPI {

    @Autowired
    private IEmpSocChangesService empSocChangesService;

    @Autowired
    private IEmpSecretKeysService empSecretKeysService;

    @Override
    @PostMapping
    public Result<EmpSocChangesVO> save(@Valid @RequestBody EmpSocChangesAddDTO empSocChangesAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工社保公积金调整记录权限校验

        //临时调用员工生成秘钥方法（生成月考勤统计中使用）
        EmpSecretKeysUpdateDTO empSecretKeysUpdateDTO = new EmpSecretKeysUpdateDTO();
        empSecretKeysUpdateDTO.setCompanyId(empSocChangesAddDTO.getCompanyId());
        empSecretKeysUpdateDTO.setEmpId(empSocChangesAddDTO.getEmpId());
        empSecretKeysUpdateDTO.setKeyType("RSA_PUB");
        try {
            empSecretKeysService.generateEmpRSA(empSecretKeysUpdateDTO, tokenUser);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //默认设置生效状态和生效时间
        empSocChangesAddDTO.setEffectiveStatus(true);
        empSocChangesAddDTO.setEffectiveDate(new Date());
        return Result.success(
                empSocChangesService.save(empSocChangesAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpSocChangesUpdateDTO empSocChangesUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工社保公积金调整记录权限校验

        int count = empSocChangesService.update(empSocChangesUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{empSocChangesId}")
    public Result<Integer> delete(@PathVariable String empSocChangesId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工社保公积金调整记录权限校验

        int count = empSocChangesService.deleteByIds(tokenUser, empSocChangesId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empSocChangesIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工社保公积金调整记录权限校验

        int count = empSocChangesService.deleteByIds(tokenUser, empSocChangesIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empSocChangesId}")
    public Result<EmpSocChangesVO> findById(@PathVariable String empSocChangesId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工社保公积金调整记录权限校验

        return Result.success(
            empSocChangesService.selectById(empSocChangesId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpSocChangesListVO>> listAll(@Valid EmpSocChangesQuery empSocChangesQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工社保公积金调整记录权限校验

        return Result.success(
            empSocChangesService.select(empSocChangesQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<EmpSocChangesListVO>> page(@Valid EmpSocChangesQuery empSocChangesQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            empSocChangesService.selectPage(empSocChangesQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/getEmpSocBasicByEmpId")
    public Result<EmpSocChangesVO> getEmpSocBasicByEmpId(String empId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工调薪记录权限校验

        return Result.success(
                empSocChangesService.getEmpSocBasicByEmpId(empId, tokenUser.getSiteId()).to(EmpSocChangesVO.class)
        );
    }

    @Override
    @PostMapping(value = "empSocPfPersonTaxDedCount")
    public Result<Boolean> empSocPfPersonTaxDedCount(@Valid @RequestBody AttendanceMonthlyAddDTO attendanceMonthlyAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增考勤扣款设置权限校验

        return Result.success(
                empSocChangesService.empSocPfPersonTaxDedCount(tokenUser, attendanceMonthlyAddDTO)
        );
    }


}

